<?php

namespace platform\models;

use cm;
use Yii;
use yii\base\Model;
use yii\helpers\Url;

class LoginModel extends Model
{
    /*
    * 登录
    */
    public function actionLogin()
    {

        Cm::$app->user->isGuest();

        if (Cm::$req->isPost()) {
            $action = Cm::$req->get('action');
            switch ($action) {
                default:
                    $account = Cm::$req->post('account', ['title' => "登录账号"]);
                    $pwd = Cm::$req->post('pwd', ['title' => "登录密码"]);
                    $pwd = md5(md5($pwd));
                    $sql = "SELECT a.* FROM sys_manager a WHERE a.`status`=1 AND a.account='{$account}' AND a.`password`='{$pwd}' ";
                    $data = Cm::$db->one($sql);
                    if (isset($data['id'])) {

                        $ip = Cm::$app->log->ip();
                        $mdata = ["last_login_time" => date('Y-m-d H:i:s'), "last_login_ip" => $ip];
                        Cm::$db->update('sys_manager',$mdata, "id={$data['id']}");

                        Cm::$app->user->setIdentity($data);

                        Cm::$app->log->authlog(1,$mdata,"登录");
                        Cm::$res->susess([], '登录成功');
                    }
                    Cm::$res->error('账号或密码错误');
                    break;
            }
        }
        return [];
    }
}